using System;
using System.Collections.Generic;
using System.Text;

namespace CourseSync.LibFetcher.Utility
{
    public class UrlTools
    {
        public static string TrimParams(string url)
        {
            if (string.IsNullOrEmpty(url))
                return string.Empty;

            int t = url.IndexOf('?');
            if (t < 0)
                return url.Trim();
            else
                return url.Substring(0, t).Trim();
        }

        public static string Combine(string parent_url, string child_url)
        {
            if (parent_url == null)
                parent_url = string.Empty;
            if (child_url == null)
                child_url = string.Empty;

            string smlchild_url = child_url.ToLower().Trim();
            int st = smlchild_url.IndexOf(":");
            if (st > 0)
            {
                string prefix = smlchild_url.Substring(0, st).Trim();
                if (prefix != "http")
                    return null;
            }

            if (child_url.StartsWith("http://"))
            {
                return Simplify(child_url);
            }

            if (child_url.StartsWith("/"))
            {
                int t = parent_url.IndexOf("://");
                if (t < 0) return null;
                t = parent_url.IndexOf('/', t + 3);
                if (t < 0) t = parent_url.Length;
                return Simplify(parent_url.Substring(0, t) + child_url);
            }
            else
            {
                parent_url = TrimParams(parent_url);
                if (!parent_url.StartsWith("http://"))
                {
                    return null;
                }
                int t = parent_url.IndexOf('/', 7);
                if (t < 0)
                {
                    return Simplify(parent_url + "/" + child_url);
                }
                else
                {
                    int t2 = parent_url.LastIndexOf('/');
                    return Simplify(parent_url.Substring(0, t2 + 1) + child_url);
                }
            }
        }

        public static string Simplify(string url)
        {
            if (string.IsNullOrEmpty(url))
                return string.Empty;

            string urlheader;
            string urlbody;
            string urlparam;
            string[] parts;
            int t = url.IndexOf("?");
            if (t > 0)
            {
                urlbody = url.Substring(0, t);
                urlparam = url.Substring(t);
            }
            else
            {
                urlbody = url;
                urlparam = string.Empty;
            }
            t = urlbody.IndexOf("://");
            if (t > 0)
            {
                urlheader = urlbody.Substring(0, t + 2);
                urlbody = urlbody.Substring(t + 3);
            }
            else
            {
                // error url
                return null;
            }
            parts = urlbody.Split('/');
            int i;
            for (i = 0; i < parts.Length; i++)
            {
                if (parts[i] == ".") parts[i] = null;
                if (parts[i] == "..")
                {
                    if (i == 0) return null;
                    int j = i - 1;
                    while (j >= 0)
                    {
                        if (parts[j] != null)
                        {
                            parts[j] = null;
                            break;
                        }
                        else
                            j--;
                    }
                    parts[i - 1] = null;
                    parts[i] = null;
                }
            }
            string retval = urlheader;
            for (i = 0; i < parts.Length; i++)
            {
                if (parts[i] != null)
                {
                    retval += "/" + parts[i];
                }
            }
            retval += urlparam;
            return retval;
        }
    }
}
